<template>
    <el-select v-model="model" placeholder="请选择" filterable style="width:100%" @change="change">
        <el-option v-for="item in list" :key="item.pId" :label="item.sNickName" :value="item.pId">
        </el-option>
    </el-select>
</template>

<script>
import { listClassBySchoolId } from 'api/common';

export default {
    props: {
        value: {
            type: [String, Number],
            required: true
        },
        schoolId: {
            type: [String, Number],
            required: true
        }
    },
    computed: {
        model: {
            get() {
                if (this.list) {
                    if (this.list.some(item => item.pId == this.value)) {
                        let classObj = this.list.find((item) => {
                            return item.pId === this.value;
                        });
                        this.$emit('getClassName',classObj.sNickName); 
                        return this.value;
                    } else {
                        this.$emit('input', '');
                        return '';
                    }
                } else {
                    return '';
                }
            },
            set(val) {
                this.$emit('input', val);
            }
        }
    },
    data() {
        return {
            list: null
        }
    },
    watch: {
        schoolId(val) {
            this.getList();
        }
    },
    created() {
        this.getList();
    },
    methods: {
        getList() {
            this.list = null;
            if (this.schoolId) {
                let schoolId = this.schoolId;
                listClassBySchoolId(this.schoolId).then(res => {
                    if (res.data.content.length && this.schoolId == schoolId) {
                        this.list = res.data.content;
                        this.list.unshift({
                            pId: '',
                            sNickName: '请选择'
                        });
                    }
                });
            } else {
                this.$emit('input', '');
            }
        },
        change(val) {
            this.$emit('change', val);
        }
    }
}
</script>

<style>

</style>
